package cn.snailhd.core.base.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;


@Repository
public class RedisDao{
	Log log = LogFactory.getLog(RedisDao.class);
	
	@Autowired
	StringRedisTemplate redisTemplate;
	
	RedisProperties redisProperties;

	
	public boolean set(String key,String value){
		try {
			redisTemplate.opsForValue().set(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			return false;
		}
	}
	
	public boolean set(String key,Object obj){
		try {
			String value = JSON.toJSONString(obj);
			return set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			return false;
		}
	}
	
	public String get(String key){
		String value = redisTemplate.opsForValue().get(key);
		return value;
	}
	
	public <T> T get(String key,Class<T> clazz){
		String value = get(key);
		return JSON.parseObject(value, clazz);
	}
	
	
	
}